package com.achievement.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.achievement.domain.dto.MagazineSourceDTO;
import com.achievement.domain.entity.MagazineSourceEntity;
import com.achievement.domain.entity.PaperLevelEntity;
import com.achievement.domain.param.MagazineSourceParam;
import com.achievement.domain.vo.MagazineSourceVO;
import com.achievement.mapper.MagazineSourceMapper;
import com.achievement.mappings.MagazineSourceMapping;
import com.achievement.service.IMagazineSourceService;
import com.achievement.service.IPaperLevelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author CNKI
 * @description 针对表【dm_magazine_source(科研成果-论文期刊源列表)】的数据库操作Service实现
 * @createDate 2025-02-28 14:53:32
 */
@Service
public class IMagazineSourceServiceImpl extends ServiceImpl<MagazineSourceMapper, MagazineSourceEntity>
        implements IMagazineSourceService {

    @Autowired
    private MagazineSourceMapping mapping;

    @Autowired
    private IPaperLevelService paperLevelService;

    /**
     * 新增论文期刊源
     *
     * @param dto dto
     */
    @Override
    public String add(MagazineSourceDTO dto) {
        String UUID = IdUtil.randomUUID().replace("-", "");

        dto.setId(UUID);

        //新增到刊物级别中
        if (StrUtil.isNotBlank(dto.getPaperLevelId())){
            String id = dto.getId();
            PaperLevelEntity paperLevelEntity = paperLevelService.getById(dto.getPaperLevelId());
            if (null == paperLevelEntity.getMagazineSourceIds()  || StrUtil.isBlank(paperLevelEntity.getMagazineSourceIds())){
                paperLevelEntity.setMagazineSourceIds(id);
            }else {
                paperLevelEntity.setMagazineSourceIds(paperLevelEntity.getMagazineSourceIds()+","+id);
            }
            paperLevelService.updateById(paperLevelEntity);
        }

        //新增论文期刊源
        MagazineSourceEntity entity = mapping.dto2Entity(dto);

        boolean save = this.save(entity);

        if (save) {
            return entity.getId();
        }

        return "";
    }

    /**
     * 删除论文期刊源
     *
     * @param ids ids
     */
    @Override
    public boolean batchRemove(List<String> ids) {

        //删除刊物级别中的期刊源
        if (ObjectUtil.isNotNull(ids)) {
            List<MagazineSourceEntity> magazineSourceEntities = this.listByIds(ids);
            for (MagazineSourceEntity entity : magazineSourceEntities) {
                String paperLevelId = entity.getPaperLevelId();

                if (ObjectUtil.isNotNull(paperLevelId)) {
                    PaperLevelEntity paperLevelEntity = paperLevelService.getById(paperLevelId);

                    List<String> list =Arrays.stream(paperLevelEntity.getMagazineSourceIds().split(",")).collect(Collectors.toList());

                    if (ObjectUtil.isNotNull(list) && list.size() > 0) {

                        list.removeIf(s -> s.equals(entity.getId()));

                        String collect = String.join(",", list);
                        paperLevelEntity.setMagazineSourceIds(collect);
                        paperLevelService.updateById(paperLevelEntity);
                    }
                }
            }
        }


        return this.removeByIds(ids);
    }

    /**
     * 修改论文期刊源
     *
     * @param dto dto
     */
    @Override
    public boolean update(MagazineSourceDTO dto) {
        MagazineSourceEntity entity = mapping.dto2Entity(dto);

        return this.updateById(entity);
    }

    /**
     * 列表检索
     *
     * @param param param
     */
    @Override
    public List<MagazineSourceVO> selectByPage(MagazineSourceParam param) {
        List<MagazineSourceVO> magazineSourceVOList = this.baseMapper.getList(param);

        return magazineSourceVOList;
    }

    /**
     * 论文期刊源导出
     *
     * @param dto dto
     */
    @Override
    public List<MagazineSourceEntity> getListByConditions(MagazineSourceDTO dto) {
        LambdaQueryWrapper<MagazineSourceEntity> queryWrapper = new LambdaQueryWrapper();
        List<MagazineSourceEntity> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 获取论文期刊源详细信息
     *
     * @param id id
     */
    @Override
    public MagazineSourceVO getInfoById(String id) {
        MagazineSourceEntity entity = this.getById(id);
        if (ObjectUtil.isNull(entity)) {
            return new MagazineSourceVO();
        }

        MagazineSourceVO magazineSourceVO = mapping.entity2Vo(entity);

        return magazineSourceVO;
    }

    /**
     * 刊物级别，绑定期刊源
     *  @param list list
     * @param paperLevelID paperLevelID
     */
    @Override
    public boolean bindPaperLevel(List<String> list, String paperLevelID) {

        LambdaUpdateWrapper<MagazineSourceEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(MagazineSourceEntity::getId,list);
        updateWrapper.set(MagazineSourceEntity::getPaperLevelId,paperLevelID);


        return this.update(updateWrapper);

    }

    /**
     * 刊物级别，删除绑定期刊源
     * @param ids ids
     */
    @Override
    public boolean deleteByPaperLevel(List<String> ids) {
        LambdaUpdateWrapper<MagazineSourceEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(MagazineSourceEntity::getPaperLevelId,ids);
        updateWrapper.set(MagazineSourceEntity::getPaperLevelId,null);

        return this.update(updateWrapper);
    }

    /**
     * 根据年份获取刊物源ID
     * @param year year
     */
    @Override
    public List<String> selectIdsByYear(String year) {

        LambdaQueryWrapper<MagazineSourceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(MagazineSourceEntity::getBeginYear,year);
        queryWrapper.ge(MagazineSourceEntity::getEndYear,year);

        List<MagazineSourceEntity> list = this.list(queryWrapper);

        if (ObjectUtil.isNotNull(list)){
            return list.stream().map(MagazineSourceEntity::getId).collect(Collectors.toList());
        }

        return new ArrayList<>();
    }
}




